//
//  MySubStoreViewController.m
//  JiFengDaRen
//
//  Created by CYF on 2017/2/16.
//  Copyright © 2017年 SH_iOS. All rights reserved.
//

#import "MySubStoreViewController.h"
@interface  MySubStoreViewController()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)NSMutableArray *dataListArr;
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,assign)int pageindex;
@property(nonatomic,assign)int counts;
@end
@implementation MySubStoreViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.pageindex = 1;
    self.counts = 10;
    [self createTableView];
    [self refresh];
}
-(void)refresh{
    
    __unsafe_unretained __typeof(self) weakSelf = self;
    
    // 设置回调（一旦进入刷新状态就会调用这个refreshingBlock）
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _pageindex = 1;
        [weakSelf requestDataIsRemove:YES];
    }];
    
    // 马上进入刷新状态
    [_tableView.mj_header beginRefreshing];
    
    
    // 设置回调（一旦进入刷新状态就会调用这个refreshingBlock）
    _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [weakSelf requestDataIsRemove:NO];
        self.pageindex ++;
        
        
    }];
    
}


#pragma mark -- 初始化数组
-(NSMutableArray *)dataListArr{
    if (!_dataListArr) {
        _dataListArr = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataListArr;
}
#define KMYSUBSTORECELL        @"mySubStore"
#pragma mark -- 创建TableView
-(void)createTableView{
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight - KNAVIGATIONBARHEIGHT) style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:KMYSUBSTORECELL];
    self.tableView.separatorStyle =   UITableViewCellSeparatorStyleNone;
    
  
}

#pragma mark -- 获取数据
-(void)requestDataIsRemove:(BOOL)isRemove{
    if (isRemove) {
        [self.dataListArr removeAllObjects];
    }
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [MyDataService requestAFURL:[NSString stringWithFormat:KGETMYSUBSTORES,self.pageindex,self.counts]   httpMethod:@"GET" params:nil data:nil complection:^(id result) {
        NSLog(@"我的商家列表%@",result);
        
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if ([result[@"success"] isEqualToNumber:@(1)] && [result[@"data"] count] != 0) {
           
//            for (NSDictionary *dict in result[@"data"]) {
////                [self.dataListArr addObject:model];
//            }
            [self.tableView reloadData];
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
        }else if([result[@"success"] isEqualToNumber:@(1)] && [result[@"data"] count] == 0){
            self.tableView.mj_footer.hidden = YES;
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
            
        }
        
        
    } failure:^(id erro) {
        [self.tableView.mj_footer endRefreshing];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        
    }];
    
    
    
    
}

#pragma mark --  UITableViewDataSourece && UITableDataSource

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataListArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:KGETMYSUBSTORES];
    if (self.dataListArr.count != 0) {
//        cell.shareProfitModel = self.dataListArr[indexPath.row];
    }
    
    return cell;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return __I6Height(65);
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  
    
    
}


@end
